package com.shaodongbo.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView iv_wifi;
    private ImageView iv_dog;
    private Button btn_start;
    private Button btn_stop;
    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_wifi = findViewById(R.id.iv_wifi);
        iv_dog = findViewById(R.id.iv_dog);
        btn_start = findViewById(R.id.btn_start);
        btn_stop = findViewById(R.id.btn_stop);

        animationDrawable = (AnimationDrawable) iv_wifi.getDrawable();

        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                animationDrawable.start();
//                让小狗执行透明度动画
//                startAlphaAnim();
//                让小狗执行平移动画
//                startTransAnim();
//                让小狗执行缩放动画
//                startScaleAnim();
//                让小狗执行旋转动画
//                startRotateAnim();
                startAnimationSetWithJava();
                startAnimationSetWithXML();
            }
        });

        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationDrawable.stop();
            }
        });
    }

    private void startAnimationSetWithXML() {
        AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation_set);
        iv_dog.startAnimation(animationSet);
    }

    private void startAnimationSetWithJava() {
        AnimationSet animationSet = new AnimationSet(true);
//        TranslateAnimation translateAnimation = new TranslateAnimation(0, 400, 0, 600);
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, 200, 200);
        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 3, 1, 3, 200, 200);
//        animationSet.addAnimation(translateAnimation);
        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.setDuration(3000);
        animationSet.setRepeatCount(5);
        animationSet.setRepeatMode(Animation.REVERSE);
        iv_dog.startAnimation(animationSet);
    }

    private void startRotateAnim() {
        RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate_anim);
//        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, 200, 200);
//        rotateAnimation.setDuration(3000);
//        rotateAnimation.setFillAfter(true);
        iv_dog.startAnimation(rotateAnimation);
    }

    private void startScaleAnim() {
        ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_anim);
//        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 3, 1, 3, 100, 100);
//        scaleAnimation.setDuration(3000);
//        scaleAnimation.setFillAfter(true);
//        scaleAnimation.setRepeatCount(5);
//        scaleAnimation.setRepeatMode(Animation.REVERSE);
        iv_dog.startAnimation(scaleAnimation);
    }

    private void startTransAnim() {
        TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate_anim);
//        TranslateAnimation translateAnimation = new TranslateAnimation(0, 300, 0, 300);
//        translateAnimation.setDuration(3000);
//        translateAnimation.setFillAfter(true);
//        translateAnimation.setRepeatCount(-1);
//        translateAnimation.setRepeatMode(Animation.REVERSE);
        iv_dog.startAnimation(translateAnimation);
    }

    private void startAlphaAnim() {
//        AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpah_anim);
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        iv_dog.startAnimation(alphaAnimation);
    }
}
